//
//  LYMineCollectTopicViewController.m
//  artist
//
//  Created by Jay_Apple on 16/5/8.
//  Copyright © 2016年 viviyoo. All rights reserved.
//

#import "LYMineCollectTopicViewController.h"
#import "LYFriendCell.h"
#import "LYCommentListViewController.h"
#import "LYPostDetailModel.h"

@interface LYMineCollectTopicViewController ()<UITableViewDataSource, UITableViewDelegate,LYFriendCellDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic, strong) NSArray *topics;

@end

@implementation LYMineCollectTopicViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self loadTopicsData];
    
    //注册通知
    [[NSNotificationCenter defaultCenter] addObserver:self.tableView selector:@selector(reloadData) name:LYImageDownloadFinished object:nil];
}

- (void)dealloc{

    [[NSNotificationCenter defaultCenter] removeObserver:self.tableView];
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    LYFriendCell *friendCell = (LYFriendCell *)cell;
    
    LYPostDetailModel *model = self.topics[indexPath.row];
    friendCell.model = model;
    friendCell.delegate = self;
    
}

#pragma mark - 网络请求

/**
 *  获取帖子
 */
- (void)loadTopicsData{
    
    [MBProgressHUD showMessage:nil];
    [LYRequestTool getMyCollectionWithToken:LYToken type:ObjectTypePost success:^(NSArray *topics) {
        [MBProgressHUD hideHUD];
        self.topics = topics;
        [self.tableView reloadData];
        
    } failure:^(NSString *code, NSString *message) {
        [MBProgressHUD hideHUD];
    }];

}

/**
 *  点赞
 */
- (void)praiseWithTopicID:(NSInteger)topicID{
    
    [LYRequestTool praiseWithToken:LYToken type:ObjectTypePost data_id:[NSString stringWithFormat:@"%zd",topicID] success:nil failure:nil];
}


/**
 *  取消点赞
 */
- (void)cancelPraiseWithTopicID:(NSInteger)topicID{
    
    [LYRequestTool cancelPraiseWithToken:LYToken type:ObjectTypePost data_id:[NSString stringWithFormat:@"%zd",topicID] success:nil failure:nil];
    
}

/**
 *  收藏
 */
- (void)collectWithTopicID:(NSInteger)topicID{
    
    [LYRequestTool collectionWithToken:LYToken type:ObjectTypePost data_id:[NSString stringWithFormat:@"%zd",topicID] success:nil failure:nil];
}


/**
 *  取消收藏
 */
- (void)cancelCollectWithTopicID:(NSInteger)topicID{
    
    
    [LYRequestTool cancelCollectionWithToken:LYToken type:ObjectTypePost data_id:[NSString stringWithFormat:@"%zd",topicID] success:nil failure:nil];
    
}
#pragma mark - friendCellDelegate

- (void)cellShowContentBtnOnclick:(LYFriendCell *)cell{
    
    cell.model.showContent = !cell.model.showContent;
    [self.tableView reloadData];
}

- (void)cell:(LYFriendCell *)cell bottomBarBtnOnclickWithType:(cellBtnType)type{
    
    [[VVYCheckStatus sharedInstance] checkIsLoginBlock:^(LYUserInfoModel *userInfo) {
        
        LYPostDetailModel *model = cell.model;
        
        switch (type) {
            case cellBtnTypeParise:
                
                model.is_praise = !model.is_praise;
                if (model.is_praise) {
                    model.praise_num++;
                    
                    [self praiseWithTopicID:model.topic_id];
                    
                }else{
                    model.praise_num--;
                    [self cancelPraiseWithTopicID:model.topic_id];
                }
                
                
                
                break;
                
            case cellBtnTypeCollect:
                
                model.is_collect = !model.is_collect;
                if (model.is_collect) {
                    model.collect_num++;
                    [self collectWithTopicID:model.topic_id];
                    
                }else{
                    model.collect_num--;
                    [self cancelCollectWithTopicID:model.topic_id];
                }
                
                break;
                
            case cellBtnTypeComment:{
                
                LYCommentListViewController *commentVC = [[LYCommentListViewController alloc] init];
                commentVC.data_id = [@(model.topic_id) stringValue];
                commentVC.type = CommentTypeTopic;
                [self.navigationController pushViewController:commentVC animated:YES];
            }
                break;
                
        }
        
        [self.tableView reloadData];
        
        
    }];
    
    
}



#pragma mark - tableViewDataSource
/**
 *  cell
 */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    LYFriendCell *cell = [LYFriendCell cellWithTableView:tableView andIndetifier:LYFriendCellIdentifier];
    
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.topics.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    LYPostDetailModel *model = self.topics[indexPath.row];
    return model.rowHeight;
}


@end
